package com.lsh.oms.core.proxy.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.proxy.model.user.UserContent;
import com.lsh.oms.core.utils.http.HttpClientTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Project Name: lsh-atp
 *
 * @author peter
 * @date 19/1/11
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Slf4j
public class UserServiceProxy {

    private static final String RET_CODE = "ret";

    private static final String SUCCESS_CODE = "0";

    private static final String USER_INFO = "/user/info/getAddressGroup";

    @Value("${proxy.user.url}")
    private String userUrl;


    public UserContent getUserInfo(String addressId, Long venderId) {

        UserContent userContent;
        try {
            String requestUrl = userUrl + USER_INFO + "?address_id=" + addressId;
            log.info("【获取用户信息】url:【{}】", requestUrl);
            Map<String, String> headMap = new HashMap<>();
            headMap.put("venderId", venderId + "");
            String resBody = HttpClientTemplate.doGet(requestUrl, new HashMap<String, Object>(3), headMap);
            log.info("【获取用户信息请求返回】{}", resBody);

            if (StringUtils.isBlank(resBody)) {
                return null;
            }

            JSONObject bodyJson = JSON.parseObject(resBody);
            if (!SUCCESS_CODE.equals(bodyJson.getString(RET_CODE))) {
                return null;
            }

            String content = bodyJson.getString("content");
            userContent = JSONObject.parseObject(content, UserContent.class);
        } catch (Exception e) {
            log.error("获取用户信息报错", e);
            return null;
        }

        return userContent;
    }

    public String getSupplierGroup(String addressId, Long venderId) {

        UserContent userContent;
        try {
            String requestUrl = userUrl + USER_INFO + "?address_id=" + addressId;
            log.info("【获取用户信息】url:【{}】", requestUrl);
            Map<String, String> headMap = new HashMap<>();
            headMap.put("venderId", venderId + "");
            String resBody = HttpClientTemplate.doGet(requestUrl, new HashMap<String, Object>(3), headMap);
            log.info("【获取用户信息请求返回】{}", resBody);

            if (StringUtils.isBlank(resBody)) {
                return null;
            }

            JSONObject bodyJson = JSON.parseObject(resBody);
            if (!SUCCESS_CODE.equals(bodyJson.getString(RET_CODE))) {
                return null;
            }

            String content = bodyJson.getString("content");
            userContent = JSONObject.parseObject(content, UserContent.class);
        } catch (Exception e) {
            log.error("获取用户信息报错", e);
            return null;
        }

        return userContent.getSupplierGroup();
    }


    public static void main(String[] args) {
        String railUrl = "http://xyc.market-api.wmdev2.lsh123.com/user/info/getAddressGroup?address_id=4052023060588234261";

        String resBody = HttpClientTemplate.doGet(railUrl);

        if (StringUtils.isBlank(resBody)) {

        }
        JSONObject bodyJson = JSON.parseObject(resBody);

        String content = bodyJson.getString("content");

        System.out.println(content);

        UserContent userContent = JSONObject.parseObject(content, UserContent.class);

        System.out.println("\n\n\n");
        System.out.println(userContent.toString());

        System.out.println("\n\n\n");
        System.out.println(JSON.toJSONString(userContent));

//        OmsProxy omsProxy = new OmsProxy();
//        UserContent userInfo = omsProxy.getUserInfo("6105523753146121662");
//        System.out.println(JSON.toJSONString(userInfo));
    }
}
